package com.payment.system.service.impl;

import com.payment.common.base.AjaxResult;
import com.payment.common.support.Convert;
import com.payment.common.type.AddressStatus;
import com.payment.common.utils.SendMessageUtils;
import com.payment.common.utils.StringUtils;
import com.payment.system.domain.ShoppingAddress;
import com.payment.system.mapper.ShoppingAddressMapper;
import com.payment.system.service.IShoppingAddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 收获地址 服务层实现
 *
 * @author payment
 * @date 2019-01-18
 */
@Service
public class ShoppingAddressServiceImpl implements IShoppingAddressService {
    private static final Logger log = LoggerFactory.getLogger(ShoppingAddressServiceImpl.class);
    @Autowired
    private ShoppingAddressMapper shoppingAddressMapper;
    /**
     * 新增收获地址
     *
     * @param shoppingAddress 收获地址信息
     * @return 结果
     */
    @Override
    public AjaxResult insertShoppingAddress(ShoppingAddress shoppingAddress) {
        AjaxResult val = this.validateAddress(shoppingAddress);
        if (Integer.parseInt(String.valueOf(val.get("code"))) != 0) {
            return val;
        }
        try {
            shoppingAddress.setId(StringUtils.randomUUID());
            shoppingAddressMapper.insertShoppingAddress(shoppingAddress);
            return AjaxResult.success("保存商户收获地址成功！");
        } catch (Exception e) {
            log.error("保存收获地址失败！", e);
            return AjaxResult.error("保存收获地址失败！");
        }
    }

    /**
     * 修改收获地址
     *
     * @param shoppingAddress 收获地址信息
     * @return 结果
     */
    @Override
    public AjaxResult updateShoppingAddress(ShoppingAddress shoppingAddress) {
        if (shoppingAddress == null || shoppingAddress.getId().length() < 0) {
            return AjaxResult.error("修改收获地址失败：缺失参数！");
        }
        AjaxResult val = this.validateAddress(shoppingAddress);
        if (Integer.parseInt(String.valueOf(val.get("code"))) != 0) {
            return val;
        }
        shoppingAddressMapper.updateShoppingAddress(shoppingAddress);
        return AjaxResult.success("修改收获地址成功！");
    }

    /**
     * 删除收获地址对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteShoppingAddressByIds(String ids) {
        return shoppingAddressMapper.deleteShoppingAddressByIds(Convert.toStrArray(ids));
    }

    //查询商户收获地址列表
    @Override
    public AjaxResult selectShoppingAddressListBymerchantId(String merchantId) {
        if (StringUtils.isEmpty(merchantId)) {
            return AjaxResult.error(1, "查询商户收获地址失败：缺失商户id");
        }
        List<ShoppingAddress> list = shoppingAddressMapper.selectShoppingAddressListBymerchantId(merchantId);
        return AjaxResult.successAndData("获取商户收获地址信息成功！", list);
    }

    //查询商户地址详情
    @Override
    public AjaxResult selectAddressById(String merchantId, String addressId) {
        if (StringUtils.isEmpty(merchantId) || StringUtils.isEmpty(addressId)) {
            return AjaxResult.error(1, "获取商户地址详情失败：缺失参数");
        }
        ShoppingAddress address = shoppingAddressMapper.selectShoppingAddressByIdAndMerchantId(merchantId, addressId);
        if (address == null) {
            return AjaxResult.error(1, "获取商户地址详情失败：暂无该商户的地址信息！");
        }
        return AjaxResult.successAndData("获取商户地址详情成功！", address);
    }

    /**
     * 设置未默认地址
     * @param merchantId
     * @param addressId
     * @return
     */
    @Transactional(rollbackFor={RuntimeException.class, Exception.class})
    @Override
    public AjaxResult updatweDefaultAddress(String merchantId, String addressId) {
        if(StringUtils.isEmpty(merchantId)|| StringUtils.isEmpty(addressId)){
            return  AjaxResult.error(1,"缺失参数");
        }
        ShoppingAddress address=shoppingAddressMapper.selectShoppingAddressById(addressId);
        if(address==null){
            return  AjaxResult.error(2,"获取地址信息失败");
        }
        List<ShoppingAddress> addressList=shoppingAddressMapper.selectShoppingAddressListBymerchantId(merchantId);
        if(addressList==null||addressList.size()==0){
            return  AjaxResult.error(3,"该用户还没有保存收获地址");
        }
        int r=shoppingAddressMapper.updategAddresAllStatusByMerchantId(merchantId,AddressStatus.NO.getCode());
        address.setStatus(AddressStatus.YES.getCode());
        int updateR=shoppingAddressMapper.updateShoppingAddress(address);
        if(updateR>0&& r>0){
            return AjaxResult.success("设置默认地址成功");
        }
        return  AjaxResult.error("设置默认地址失败");
    }

    protected AjaxResult validateAddress(ShoppingAddress shoppingAddress) {
        if (shoppingAddress == null || shoppingAddress.getMerchantId().equals("")) {
            return AjaxResult.error(1, "获取用户信息失败！");
        }
        if (shoppingAddress.getProvince().equals("") || shoppingAddress.getCity().equals("") || shoppingAddress.getArea().equals("")) {
            return AjaxResult.error(1, "收获地址不能为空！");
        }
        if (shoppingAddress.getAddressDetails().equals("")) {
            return AjaxResult.error(1, "详细地址不能为空！");
        }
        if (shoppingAddress.getPostCode().length() != 6) {
            return AjaxResult.error(1, "邮编不正确！");
        }
        if (!SendMessageUtils.isPhone(shoppingAddress.getPhone())) {
            return AjaxResult.error(1, "手机号格式不正确！");
        }
        if (shoppingAddress.getUserName().equals("") || shoppingAddress.getPhone().equals("")) {
            return AjaxResult.error(1, "收获人信息不能为空！");
        }
        if (shoppingAddress.getStatus() == null || !shoppingAddress.getStatus().equals(AddressStatus.YES.getCode())) {
            shoppingAddress.setStatus(AddressStatus.NO.getCode());
        }
        if (shoppingAddress.getStatus() != null && shoppingAddress.getStatus().equals(AddressStatus.YES.getCode())) {
          shoppingAddressMapper.updategAddresAllStatusByMerchantId(shoppingAddress.getMerchantId(),AddressStatus.NO.getCode());
        }
        return AjaxResult.success();
    }
}
